<?php

namespace apiend\modules\passport\models;

use Yii;
use common\logics\DashboardLogic;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;

/**
 * 安全中心面板信息
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SecurityDashboardLogic extends DashboardLogic {

    /**
     * @var array 安全评估
     */
    public $assess = null;

    /**
     * @var array 安全条目选项
     */
    public $items = [];

    /**
     * 安全条目选项
     * @return array
     */
    public function setItems() {
        if (!empty($this->items)) {
            return $this->items;
        }
        $model = new SecurityAccountModel();
        $model->user = Yii::$app->user->identity;
        $data = $model->toArray();
        $options = [];
        foreach ($data as $name => $item) {
            if (empty($item)) {
                continue;
            }
            $item['name'] = $name;
            $options[] = $item;
        }
        return $options;
    }

    /**
     * 安全评估值
     * @return array
     */
    public function setAssess() {
        $items = $this->setItems();
        $credits = ArrayHelper::getColumn($items, 'credit');
        $count = count($credits);
        $point = 0;
        if ($count) {
            $point = array_sum($credits) / $count;
        }
        return [
            'percentage' => $point
        ];
    }

}
